package cn.schoolwow.quickdao.domain.provider;

import cn.schoolwow.quickdao.domain.database.parent.DatabaseType;

public class PostgresDatabaseProvider implements DatabaseProvider {
    @Override
    public String name() {
        return "postgresql";
    }

    @Override
    public DatabaseType getDatabaseType() {
        return DatabaseType.Postgresql;
    }

    @Override
    public String comment(String comment) {
        return "";
    }

    @Override
    public String escape(String value) {
        return "\"" + value + "\"";
    }

    @Override
    public boolean returnGeneratedKeys() {
        return true;
    }

    @Override
    public String convertJavaTypeToColumnType(String javaType) {
        switch (javaType){
            case "byte":
            case "java.lang.Byte":{return "BOOLEAN";}
            case "[B":{return "BIT";}
            case "boolean":
            case "java.lang.Boolean":{return "BOOLEAN";}
            case "char":{return "CHAR";}
            case "java.lang.Character":{return "CHARACTER";}
            case "short":
            case "java.lang.Short":{return "SMALLINT";}
            case "int":{return "INT4";}
            case "java.lang.Integer":{return "INTEGER";}
            case "float":
            case "java.lang.Float":{return "FLOAT4";}
            case "long":
            case "java.lang.Long":{return "INT8";}
            case "double":
            case "java.lang.Double":{return "FLOAT8";}
            case "java.lang.String":{return "VARCHAR(255)";}
            case "java.util.Date":{return "TIMESTAMP";}
            case "java.sql.Date":{return "DATE";}
            case "java.sql.Time":{return "TIME";}
            case "java.sql.Timestamp":{return "TIMESTAMP";}
            case "java.time.LocalDate":{return "DATE";}
            case "java.time.LocalDateTime":{return "TIMESTAMP";}
            case "java.math.BigDecimal":{return "DECIMAL";}
            case "java.sql.Blob":{return "TEXT";}
            case "java.sql.Clob":{return "TEXT";}
            case "java.sql.NClob":{return "TEXT";}
            case "java.io.InputStream":
            case "java.io.Reader":{return "TEXT";}
            case "com.alibaba.fastjson.JSONObject":
            case "com.alibaba.fastjson.JSONArray":{return "JSONB";}
        }
        throw new IllegalArgumentException("目前不支持转换java数据类型["+javaType+"]为数据库类型");
    }

    @Override
    public String convertColumnTypeToJavaType(String columnType) {
        String rawColumnType = columnType;
        if(columnType.contains("(")){
            columnType = columnType.substring(0, columnType.indexOf("("));
        }
        columnType = columnType.trim().toUpperCase();
        switch (columnType){
            case "BOOL":
            case "BOOLEAN":{return "Boolean";}
            case "CHAR":{return "char";}
            case "CHARACTER":{return "Character";}
            case "INT4":{return "int";}
            case "INTEGER":{return "Integer";}
            case "INT8":{return "Long";}
            case "FLOAT4":{return "Float";}
            case "FLOAT8":{return "Double";}
            case "BIT":{return "[B";}
            case "VARCHAR":{return "String";}
            case "BPCHAR":{return "String";}
            case "TIMESTAMP":{return "java.time.LocalDateTime";}
            case "DATE":{return "java.time.LocalDate";}
            case "TIME":{return "java.sql.Time";}
            case "DECIMAL":{return "java.math.BigDecimal";}
            case "TEXT":{return "String";}
            case "JSONB":{return "com.alibaba.fastjson.JSONObject";}
        }
        throw new IllegalArgumentException("目前不支持转换数据库类型["+rawColumnType+"]为java数据类型");
    }

}
